﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;

namespace Model
{
	public class HYConduitConnect
	{
		public FamilyInstance NewElbowFitting(IList<Conduit> conduits)
		{
			FamilyInstance familyInstance = null;
			FamilyInstance result;
			try
			{
				if (conduits.Count < 2)
				{
					throw new System.ArgumentNullException("Conduit", "is null");
				}
				Document document = conduits[0].Document;
				HYCableTray<Conduit> hycableTray = new HYCableTray<Conduit>(conduits[0]);
				HYCableTray<Conduit> hycableTray2 = new HYCableTray<Conduit>(conduits[1]);
				XYZ xyz = hycableTray.IntersectionPoint(hycableTray2.CableTray);
				if (xyz != null)
				{
					Connector appropriateConnector = hycableTray.GetAppropriateConnector(xyz);
					Connector appropriateConnector2 = hycableTray2.GetAppropriateConnector(xyz);
					if (appropriateConnector == null || appropriateConnector2 == null || appropriateConnector.IsConnected || appropriateConnector2.IsConnected)
					{
						throw new Exception("线管已经连接");
					}
					familyInstance = document.Create.NewElbowFitting(appropriateConnector, appropriateConnector2);
				}
				result = familyInstance;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			return result;
		}

		public FamilyInstance NewTransitionFitting(IList<Conduit> conduits)
		{
			FamilyInstance familyInstance = null;
			FamilyInstance result;
			try
			{
				if (conduits.Count < 2)
				{
					throw new System.ArgumentNullException("CableTray", "is null");
				}
				Document document = conduits[0].Document;
				HYCableTray<Conduit> hycableTray = new HYCableTray<Conduit>(conduits[0]);
				IList<Connector> list = new List<Connector>();
				list = hycableTray.GetAppropriateConnector(conduits[1]);
				if (list.Count > 1 && list[0] != null && list[1] != null && !list[0].IsConnected && !list[1].IsConnected)
				{
					familyInstance = document.Create.NewTransitionFitting(list[0], list[1]);
				}
				document.Regenerate();
				result = familyInstance;
			}
			catch (Exception)
			{
				familyInstance = null;
				result = familyInstance;
			}
			return result;
		}

		public FamilyInstance NewTeeFitting(IList<Conduit> conduits)
		{
			FamilyInstance familyInstance = null;
			SubTransaction subTransaction = null;
			FamilyInstance result;
			try
			{
				if (conduits.Count < 3)
				{
					throw new System.ArgumentNullException("Conduit", "is null");
				}
				Document document = conduits[0].Document;
				new List<Conduit>();
				IList<Conduit> list = this.OrderConduitPosition(conduits);
				HYCableTray<Conduit> hycableTray = new HYCableTray<Conduit>(list[0]);
				HYCableTray<Conduit> hycableTray2 = new HYCableTray<Conduit>(list[1]);
				HYCableTray<Conduit> hycableTray3 = new HYCableTray<Conduit>(list[2]);
				HYCableTray<Conduit> hycableTray4 = null;
				subTransaction = new SubTransaction(document);
				subTransaction.Start();
				XYZ xyz = hycableTray.IntersectionPoint(hycableTray2.CableTray);
				if (xyz != null)
				{
					if (hycableTray2.CableTray.UniqueId == hycableTray3.CableTray.UniqueId)
					{
						new List<Conduit>();
						hycableTray4 = new HYCableTray<Conduit>(hycableTray2.Separate(xyz)[1]);
					}
					else if (hycableTray2.IsCableTrayOnLine(hycableTray3.CableTray) || hycableTray2.IsParallel(hycableTray3.CableTray))
					{
						hycableTray4 = new HYCableTray<Conduit>(hycableTray3.CableTray);
					}
					if (hycableTray4 == null)
					{
						throw new Exception("请检查线管位置是否正确");
					}
					Connector appropriateConnector = hycableTray.GetAppropriateConnector(xyz);
					Connector appropriateConnector2 = hycableTray2.GetAppropriateConnector(xyz);
					Connector appropriateConnector3 = hycableTray4.GetAppropriateConnector(xyz);
					if (appropriateConnector == null || appropriateConnector.IsConnected || appropriateConnector2 == null || appropriateConnector2.IsConnected || appropriateConnector3 == null || appropriateConnector3.IsConnected)
					{
						throw new Exception("线管已经连接");
					}
					familyInstance = document.Create.NewTeeFitting(appropriateConnector2, appropriateConnector3, appropriateConnector);
				}
				document.Regenerate();
				subTransaction.Commit();
				result = familyInstance;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				if (subTransaction != null)
				{
					TransactionStatus status = subTransaction.GetStatus();
					if (3 != (int)status)
					{
						subTransaction.RollBack();
					}
				}
			}
			return result;
		}

		public FamilyInstance NewCrossFitting(IList<Conduit> conduits)
		{
			FamilyInstance familyInstance = null;
			SubTransaction subTransaction = null;
			FamilyInstance result;
			try
			{
				if (conduits.Count < 4)
				{
					throw new System.ArgumentNullException("Conduit", "is null");
				}
				Document document = conduits[0].Document;
				new List<Conduit>();
				IList<Conduit> list = this.OrderConduitPosition(conduits);
				HYCableTray<Conduit> hycableTray = new HYCableTray<Conduit>(list[0]);
				HYCableTray<Conduit> hycableTray2 = new HYCableTray<Conduit>(list[1]);
				HYCableTray<Conduit> hycableTray3 = new HYCableTray<Conduit>(list[2]);
				HYCableTray<Conduit> hycableTray4 = new HYCableTray<Conduit>(list[3]);
				HYCableTray<Conduit> hycableTray5 = null;
				HYCableTray<Conduit> hycableTray6 = null;
				subTransaction = new SubTransaction(document);
				subTransaction.Start();
				XYZ xyz = hycableTray.IntersectionPoint(hycableTray3.CableTray);
				if (xyz != null)
				{
					if (hycableTray.CableTray.UniqueId == hycableTray2.CableTray.UniqueId)
					{
						new List<Conduit>();
						hycableTray5 = new HYCableTray<Conduit>(hycableTray.Separate(xyz)[1]);
					}
					else if (hycableTray.IsCableTrayOnLine(hycableTray2.CableTray) || hycableTray.IsParallel(hycableTray2.CableTray))
					{
						hycableTray5 = new HYCableTray<Conduit>(hycableTray2.CableTray);
					}
					if (hycableTray3.CableTray.UniqueId == hycableTray4.CableTray.UniqueId)
					{
						new List<Conduit>();
						hycableTray6 = new HYCableTray<Conduit>(hycableTray3.Separate(xyz)[1]);
					}
					else if (hycableTray3.IsCableTrayOnLine(hycableTray4.CableTray) || hycableTray3.IsParallel(hycableTray4.CableTray))
					{
						hycableTray6 = new HYCableTray<Conduit>(hycableTray4.CableTray);
					}
					if (hycableTray5 == null || hycableTray6 == null)
					{
						throw new Exception("请检查线管位置是否正确");
					}
					Connector appropriateConnector = hycableTray.GetAppropriateConnector(xyz);
					Connector appropriateConnector2 = hycableTray5.GetAppropriateConnector(xyz);
					Connector appropriateConnector3 = hycableTray3.GetAppropriateConnector(xyz);
					Connector appropriateConnector4 = hycableTray6.GetAppropriateConnector(xyz);
					if (appropriateConnector == null || appropriateConnector.IsConnected || appropriateConnector2 == null || appropriateConnector2.IsConnected || appropriateConnector3 == null || appropriateConnector3.IsConnected || appropriateConnector4 == null || appropriateConnector4.IsConnected)
					{
						throw new Exception("线管已经连接");
					}
					familyInstance = document.Create.NewCrossFitting(appropriateConnector, appropriateConnector2, appropriateConnector3, appropriateConnector4);
				}
				subTransaction.Commit();
				result = familyInstance;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				if (subTransaction != null)
				{
					TransactionStatus status = subTransaction.GetStatus();
					if (3 != (int)status)
					{
						subTransaction.RollBack();
					}
				}
			}
			return result;
		}

		public bool NewConduitConnection(IList<Conduit> conduits)
		{
			SubTransaction subTransaction = null;
			bool flag = false;
			bool result;
			try
			{
				if (conduits.Count < 2)
				{
					throw new System.ArgumentNullException("Conduit", "is null");
				}
				Document document = conduits[0].Document;
				HYCableTray<Conduit> hycableTray = new HYCableTray<Conduit>(conduits[0]);
				HYCableTray<Conduit> hycableTray2 = new HYCableTray<Conduit>(conduits[1]);
				IList<Conduit> list = new List<Conduit>();
				IList<HYCableTray<Conduit>> list2 = new List<HYCableTray<Conduit>>();
				list2.Add(hycableTray);
				list2.Add(hycableTray2);
				subTransaction = new SubTransaction(document);
				subTransaction.Start();
				switch (hycableTray.CheckPipingsPosition(hycableTray2.CableTray))
				{
				case 10:
				{
					bool flag2 = hycableTray.CustomConnectorStart.Connector.Radius == hycableTray2.CustomConnectorStart.Connector.Radius;
					if (flag2)
					{
						if (hycableTray.Combine(hycableTray2.CableTray))
						{
							flag = true;
							goto IL_A93;
						}
						goto IL_A93;
					}
					else
					{
						list.Add(hycableTray.CableTray);
						list.Add(hycableTray2.CableTray);
						if (this.NewTransitionFitting(list) != null)
						{
							flag = true;
							goto IL_A93;
						}
						goto IL_A93;
					}
					break;
				}
				case 11:
				{
					bool flag3 = true;
					IList<XYZ> appAndEndPoint = hycableTray.GetAppAndEndPoint(hycableTray2.CableTray, ref flag3);
					if (appAndEndPoint != null && appAndEndPoint.Count > 0)
					{
						if (!flag3)
						{
							XYZ value = appAndEndPoint[0];
							appAndEndPoint[0] = appAndEndPoint[1];
							appAndEndPoint[1] = value;
						}
						if (hycableTray.IsPointOnCableTrayCurve(appAndEndPoint[1]) && !hycableTray.CustomConnectorStart.XYZ.IsAlmostEqualTo(appAndEndPoint[1]) && !hycableTray.CustomConnectorEnd.XYZ.IsAlmostEqualTo(appAndEndPoint[1]))
						{
							Conduit item = HYConduitConnect.NewConduit(hycableTray2.CableTray, appAndEndPoint[0], appAndEndPoint[1]);
							list.Add(item);
							list.Add(hycableTray2.CableTray);
							this.NewElbowFitting(list);
							list.Clear();
							list.Add(item);
							list.Add(hycableTray.CableTray);
							list.Add(hycableTray.CableTray);
							this.NewTeeFitting(list);
						}
						else if (hycableTray2.IsPointOnCableTrayCurve(appAndEndPoint[1]) && !hycableTray2.CustomConnectorStart.XYZ.IsAlmostEqualTo(appAndEndPoint[1]) && !hycableTray2.CustomConnectorEnd.XYZ.IsAlmostEqualTo(appAndEndPoint[1]))
						{
							Conduit item = HYConduitConnect.NewConduit(hycableTray.CableTray, appAndEndPoint[0], appAndEndPoint[1]);
							list.Add(item);
							list.Add(hycableTray.CableTray);
							this.NewElbowFitting(list);
							list.Clear();
							list.Add(item);
							list.Add(hycableTray2.CableTray);
							list.Add(hycableTray2.CableTray);
							this.NewTeeFitting(list);
						}
						else
						{
							Conduit item = HYConduitConnect.NewConduit(hycableTray.CableTray, appAndEndPoint[0], appAndEndPoint[1]);
							list.Add(item);
							list.Add(hycableTray.CableTray);
							this.NewElbowFitting(list);
							list.Clear();
							list.Add(item);
							list.Add(hycableTray2.CableTray);
							this.NewElbowFitting(list);
						}
						flag = true;
						document.Regenerate();
						goto IL_A93;
					}
					IList<Connector> appropriateConnector = hycableTray.GetAppropriateConnector(hycableTray2.CableTray);
					if (appropriateConnector != null && appropriateConnector.Count > 1)
					{
						XYZ point = (appropriateConnector[0].Origin + appropriateConnector[1].Origin) / 2.0;
						XYZ lineProject = hycableTray.GetLineProject(point);
						XYZ lineProject2 = hycableTray2.GetLineProject(point);
						HYCableTray<Conduit> hycableTray3 = new HYCableTray<Conduit>(HYConduitConnect.NewConduit(hycableTray.CableTray, lineProject, lineProject2));
						list.Add(hycableTray.CableTray);
						list.Add(hycableTray3.CableTray);
						this.NewElbowFitting(list);
						document.Regenerate();
						list.Clear();
						list.Add(hycableTray2.CableTray);
						list.Add(hycableTray3.CableTray);
						this.NewElbowFitting(list);
						flag = true;
						document.Regenerate();
						goto IL_A93;
					}
					goto IL_A93;
				}
				case 12:
					list.Add(hycableTray.CableTray);
					list.Add(hycableTray2.CableTray);
					this.NewElbowFitting(list);
					flag = true;
					goto IL_A93;
				case 13:
				{
					list.Add(hycableTray.CableTray);
					list.Add(hycableTray2.CableTray);
					Conduit pointOnConduit = this.GetPointOnConduit(list);
					if (pointOnConduit != null)
					{
						list.Add(pointOnConduit);
					}
					this.NewTeeFitting(list);
					flag = true;
					goto IL_A93;
				}
				case 14:
					list.Add(hycableTray.CableTray);
					list.Add(hycableTray.CableTray);
					list.Add(hycableTray2.CableTray);
					list.Add(hycableTray2.CableTray);
					this.NewCrossFitting(list);
					flag = true;
					goto IL_A93;
				case 21:
				{
					bool flag3 = true;
					IList<XYZ> appAndEndPoint2 = hycableTray.GetAppAndEndPoint(hycableTray2.CableTray, ref flag3);
					if (appAndEndPoint2 != null && appAndEndPoint2.Count > 0)
					{
						if (!flag3)
						{
							XYZ value2 = appAndEndPoint2[0];
							appAndEndPoint2[0] = appAndEndPoint2[1];
							appAndEndPoint2[1] = value2;
						}
						XYZ endXYZ = new XYZ(appAndEndPoint2[1].X, appAndEndPoint2[1].Y, appAndEndPoint2[0].Z);
						if (hycableTray.IsPointOnCableTrayCurve(appAndEndPoint2[1]))
						{
							Conduit item2 = HYConduitConnect.NewConduit(hycableTray2.CableTray, appAndEndPoint2[0], endXYZ);
							Conduit item3 = HYConduitConnect.NewConduit(hycableTray2.CableTray, appAndEndPoint2[1], endXYZ);
							list.Add(hycableTray2.CableTray);
							list.Add(item2);
							this.NewElbowFitting(list);
							document.Regenerate();
							list.Clear();
							list.Add(item2);
							list.Add(item3);
							this.NewElbowFitting(list);
							document.Regenerate();
							list.Clear();
							list.Add(hycableTray.CableTray);
							list.Add(hycableTray.CableTray);
							list.Add(item3);
							this.NewTeeFitting(list);
						}
						else
						{
							Conduit item4 = HYConduitConnect.NewConduit(hycableTray.CableTray, appAndEndPoint2[0], endXYZ);
							Conduit item5 = HYConduitConnect.NewConduit(hycableTray.CableTray, appAndEndPoint2[1], endXYZ);
							list.Add(hycableTray.CableTray);
							list.Add(item4);
							this.NewElbowFitting(list);
							document.Regenerate();
							list.Clear();
							list.Add(item4);
							list.Add(item5);
							this.NewElbowFitting(list);
							document.Regenerate();
							list.Clear();
							list.Add(hycableTray2.CableTray);
							list.Add(hycableTray2.CableTray);
							list.Add(item5);
							this.NewTeeFitting(list);
						}
						flag = true;
						goto IL_A93;
					}
					IList<XYZ> point2 = this.GetPoint21(list2);
					if (point2 != null && point2.Count > 0)
					{
						Conduit item6 = HYConduitConnect.NewConduit(hycableTray.CableTray, point2[0], point2[2]);
						Conduit item7 = HYConduitConnect.NewConduit(hycableTray.CableTray, point2[1], point2[2]);
						if (hycableTray.IsPointOnCableTrayLine(point2[0]))
						{
							list.Add(hycableTray.CableTray);
							list.Add(item6);
							this.NewElbowFitting(list);
							document.Regenerate();
							list.Clear();
							list.Add(item6);
							list.Add(item7);
							this.NewElbowFitting(list);
							document.Regenerate();
							list.Clear();
							list.Add(item7);
							list.Add(hycableTray2.CableTray);
							this.NewElbowFitting(list);
							document.Regenerate();
						}
						else
						{
							list.Add(hycableTray.CableTray);
							list.Add(item7);
							this.NewElbowFitting(list);
							document.Regenerate();
							list.Clear();
							list.Add(item6);
							list.Add(item7);
							this.NewElbowFitting(list);
							document.Regenerate();
							list.Clear();
							list.Add(item6);
							list.Add(hycableTray2.CableTray);
							this.NewElbowFitting(list);
							document.Regenerate();
						}
						flag = true;
						goto IL_A93;
					}
					goto IL_A93;
				}
				case 22:
				{
					List<HYCableTray<Conduit>> newCableTray = this.GetNewCableTray(list2, document);
					list.Add(hycableTray.CableTray);
					list.Add(newCableTray[0].CableTray);
					this.NewElbowFitting(list);
					list.Clear();
					list.Add(hycableTray2.CableTray);
					list.Add(newCableTray[1].CableTray);
					this.NewElbowFitting(list);
					flag = true;
					goto IL_A93;
				}
				case 23:
				{
					List<HYCableTray<Conduit>> newCableTray = this.GetNewCableTray(list2, document);
					if (hycableTray.IsPointOnCableTrayCurve(newCableTray[0].CustomConnectorStart.XYZ) || hycableTray.IsPointOnCableTrayCurve(newCableTray[0].CustomConnectorEnd.XYZ))
					{
						list.Add(hycableTray.CableTray);
						list.Add(hycableTray.CableTray);
						list.Add(newCableTray[0].CableTray);
						this.NewTeeFitting(list);
						list.Clear();
						list.Add(hycableTray2.CableTray);
						list.Add(newCableTray[1].CableTray);
						this.NewElbowFitting(list);
					}
					else
					{
						list.Add(hycableTray.CableTray);
						list.Add(newCableTray[0].CableTray);
						this.NewElbowFitting(list);
						list.Clear();
						list.Add(hycableTray2.CableTray);
						list.Add(hycableTray2.CableTray);
						list.Add(newCableTray[1].CableTray);
						this.NewTeeFitting(list);
					}
					flag = true;
					goto IL_A93;
				}
				case 24:
				{
					List<HYCableTray<Conduit>> newCableTray = this.GetNewCableTray(list2, document);
					list.Add(hycableTray.CableTray);
					list.Add(hycableTray.CableTray);
					list.Add(newCableTray[0].CableTray);
					this.NewTeeFitting(list);
					list.Clear();
					list.Add(hycableTray2.CableTray);
					list.Add(hycableTray2.CableTray);
					list.Add(newCableTray[1].CableTray);
					this.NewTeeFitting(list);
					flag = true;
					goto IL_A93;
				}
				}
				flag = false;
				IL_A93:
				document.Regenerate();
				subTransaction.Commit();
				result = flag;
			}
			catch (Exception)
			{
				flag = false;
				result = flag;
			}
			finally
			{
				if (subTransaction != null)
				{
					TransactionStatus status = subTransaction.GetStatus();
					if (3 != (int)status)
					{
						subTransaction.RollBack();
					}
				}
			}
			return result;
		}

		public bool NewSomeConnect(IList<Conduit> conduits)
		{
			bool flag = true;
			bool result;
			try
			{
				if (conduits == null || conduits.Count < 2)
				{
					throw new Exception("最少选择2根线管");
				}
				Document document = conduits[0].Document;
				IList<HYCableTray<Conduit>> list = new List<HYCableTray<Conduit>>();
				list = this.SomeConduitOrderBy(conduits);
				if (list != null && list.Count > 1)
				{
					flag = this.ConduitsConnect(list, document);
				}
				result = flag;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			return result;
		}

		private bool ConduitsConnect(IList<HYCableTray<Conduit>> orderCableTrays, Document doc)
		{
			bool result = false;
			if (orderCableTrays.Count > 1)
			{
				IList<Conduit> list = new List<Conduit>();
				for (int i = 0; i < orderCableTrays.Count; i += 2)
				{
					HYCableTray<Conduit> hycableTray;
					HYCableTray<Conduit> hycableTray2;
					if (orderCableTrays[i].CableTray.get_Parameter(BuiltInParameter.RBS_OFFSET_PARAM).AsDouble() > orderCableTrays[i + 1].CableTray.get_Parameter(BuiltInParameter.RBS_OFFSET_PARAM).AsDouble())
					{
						hycableTray = new HYCableTray<Conduit>(orderCableTrays[i].CableTray);
						hycableTray2 = new HYCableTray<Conduit>(orderCableTrays[i + 1].CableTray);
					}
					else
					{
						hycableTray = new HYCableTray<Conduit>(orderCableTrays[i + 1].CableTray);
						hycableTray2 = new HYCableTray<Conduit>(orderCableTrays[i].CableTray);
					}
					list.Clear();
					IList<HYCableTray<Conduit>> list2 = new List<HYCableTray<Conduit>>();
					list2.Add(hycableTray);
					list2.Add(hycableTray2);
					switch (hycableTray.CheckPipingsPosition(hycableTray2.CableTray))
					{
					case 10:
					{
						bool flag = hycableTray.CustomConnectorStart.Connector.Radius == hycableTray2.CustomConnectorStart.Connector.Radius;
						if (flag)
						{
							hycableTray.Combine(hycableTray2.CableTray);
						}
						else
						{
							list.Add(hycableTray.CableTray);
							list.Add(hycableTray2.CableTray);
							this.NewTransitionFitting(list);
							doc.Regenerate();
						}
						break;
					}
					case 11:
					{
						bool flag2 = true;
						IList<XYZ> appAndEndPoint = hycableTray.GetAppAndEndPoint(hycableTray2.CableTray, ref flag2);
						if (appAndEndPoint != null && appAndEndPoint.Count > 0)
						{
							if (!flag2)
							{
								XYZ value = appAndEndPoint[0];
								appAndEndPoint[0] = appAndEndPoint[1];
								appAndEndPoint[1] = value;
							}
							if (hycableTray.IsPointOnCableTrayCurve(appAndEndPoint[1]) && !hycableTray.CustomConnectorStart.XYZ.IsAlmostEqualTo(appAndEndPoint[1]) && !hycableTray.CustomConnectorEnd.XYZ.IsAlmostEqualTo(appAndEndPoint[1]))
							{
								Conduit item = HYConduitConnect.NewConduit(hycableTray2.CableTray, appAndEndPoint[0], appAndEndPoint[1]);
								list.Add(item);
								list.Add(hycableTray2.CableTray);
								this.NewElbowFitting(list);
								list.Clear();
								list.Add(item);
								list.Add(hycableTray.CableTray);
								list.Add(hycableTray.CableTray);
								this.NewTeeFitting(list);
							}
							else if (hycableTray2.IsPointOnCableTrayCurve(appAndEndPoint[1]) && !hycableTray2.CustomConnectorStart.XYZ.IsAlmostEqualTo(appAndEndPoint[1]) && !hycableTray2.CustomConnectorEnd.XYZ.IsAlmostEqualTo(appAndEndPoint[1]))
							{
								Conduit item = HYConduitConnect.NewConduit(hycableTray.CableTray, appAndEndPoint[0], appAndEndPoint[1]);
								list.Add(item);
								list.Add(hycableTray.CableTray);
								this.NewElbowFitting(list);
								list.Clear();
								list.Add(item);
								list.Add(hycableTray2.CableTray);
								list.Add(hycableTray2.CableTray);
								this.NewTeeFitting(list);
							}
							else
							{
								Conduit item = HYConduitConnect.NewConduit(hycableTray.CableTray, appAndEndPoint[0], appAndEndPoint[1]);
								list.Add(item);
								list.Add(hycableTray.CableTray);
								this.NewElbowFitting(list);
								list.Clear();
								list.Add(item);
								list.Add(hycableTray2.CableTray);
								this.NewElbowFitting(list);
							}
						}
						else
						{
							IList<Connector> appropriateConnector = hycableTray.GetAppropriateConnector(hycableTray2.CableTray);
							if (appropriateConnector != null && appropriateConnector.Count > 1)
							{
								XYZ point = (appropriateConnector[0].Origin + appropriateConnector[1].Origin) / 2.0;
								XYZ lineProject = hycableTray.GetLineProject(point);
								XYZ lineProject2 = hycableTray2.GetLineProject(point);
								HYCableTray<Conduit> hycableTray3 = new HYCableTray<Conduit>(HYConduitConnect.NewConduit(hycableTray.CableTray, lineProject, lineProject2));
								list.Add(hycableTray.CableTray);
								list.Add(hycableTray3.CableTray);
								this.NewElbowFitting(list);
								doc.Regenerate();
								list.Clear();
								list.Add(hycableTray2.CableTray);
								list.Add(hycableTray3.CableTray);
								this.NewElbowFitting(list);
								doc.Regenerate();
							}
						}
						break;
					}
					case 12:
						list.Add(hycableTray.CableTray);
						list.Add(hycableTray2.CableTray);
						this.NewElbowFitting(list);
						break;
					case 13:
					{
						list.Add(hycableTray.CableTray);
						list.Add(hycableTray2.CableTray);
						Conduit pointOnConduit = this.GetPointOnConduit(list);
						if (pointOnConduit != null)
						{
							list.Add(pointOnConduit);
						}
						this.NewTeeFitting(list);
						break;
					}
					case 14:
						list.Add(hycableTray.CableTray);
						list.Add(hycableTray.CableTray);
						list.Add(hycableTray2.CableTray);
						list.Add(hycableTray2.CableTray);
						this.NewCrossFitting(list);
						break;
					case 21:
					{
						bool flag3 = true;
						IList<XYZ> appAndEndPoint2 = hycableTray.GetAppAndEndPoint(hycableTray2.CableTray, ref flag3);
						if (appAndEndPoint2 != null && appAndEndPoint2.Count > 0)
						{
							XYZ endXYZ = new XYZ(appAndEndPoint2[1].X, appAndEndPoint2[1].Y, appAndEndPoint2[0].Z);
							if (flag3)
							{
								Conduit item2 = HYConduitConnect.NewConduit(hycableTray2.CableTray, appAndEndPoint2[0], endXYZ);
								Conduit item3 = HYConduitConnect.NewConduit(hycableTray2.CableTray, appAndEndPoint2[1], endXYZ);
								list.Add(hycableTray.CableTray);
								list.Add(item2);
								this.NewElbowFitting(list);
								doc.Regenerate();
								list.Clear();
								list.Add(item2);
								list.Add(item3);
								this.NewElbowFitting(list);
								doc.Regenerate();
								list.Clear();
								if (this.IsCreateElbow(hycableTray2, appAndEndPoint2[1]))
								{
									list.Add(hycableTray2.CableTray);
									list.Add(item3);
									this.NewElbowFitting(list);
								}
								else
								{
									list.Add(hycableTray2.CableTray);
									list.Add(hycableTray2.CableTray);
									list.Add(item3);
									this.NewTeeFitting(list);
								}
							}
							else
							{
								Conduit item4 = HYConduitConnect.NewConduit(hycableTray.CableTray, appAndEndPoint2[0], endXYZ);
								Conduit item5 = HYConduitConnect.NewConduit(hycableTray.CableTray, appAndEndPoint2[1], endXYZ);
								list.Add(hycableTray2.CableTray);
								list.Add(item5);
								this.NewElbowFitting(list);
								doc.Regenerate();
								list.Clear();
								list.Add(item4);
								list.Add(item5);
								this.NewElbowFitting(list);
								doc.Regenerate();
								list.Clear();
								if (this.IsCreateElbow(hycableTray, appAndEndPoint2[0]))
								{
									list.Add(hycableTray.CableTray);
									list.Add(item4);
									this.NewElbowFitting(list);
								}
								else
								{
									list.Add(hycableTray.CableTray);
									list.Add(hycableTray.CableTray);
									list.Add(item4);
									this.NewTeeFitting(list);
								}
							}
						}
						else
						{
							IList<XYZ> point2 = this.GetPoint21(list2);
							if (point2 != null && point2.Count > 0)
							{
								Conduit item6 = HYConduitConnect.NewConduit(hycableTray.CableTray, point2[0], point2[2]);
								Conduit item7 = HYConduitConnect.NewConduit(hycableTray.CableTray, point2[1], point2[2]);
								if (hycableTray.IsPointOnCableTrayLine(point2[0]))
								{
									list.Add(hycableTray.CableTray);
									list.Add(item6);
									this.NewElbowFitting(list);
									doc.Regenerate();
									list.Clear();
									list.Add(item6);
									list.Add(item7);
									this.NewElbowFitting(list);
									doc.Regenerate();
									list.Clear();
									list.Add(item7);
									list.Add(hycableTray2.CableTray);
									this.NewElbowFitting(list);
									doc.Regenerate();
								}
							}
						}
						break;
					}
					case 22:
					{
						List<HYCableTray<Conduit>> newCableTray = this.GetNewCableTray(list2, doc);
						list.Add(hycableTray.CableTray);
						list.Add(newCableTray[0].CableTray);
						this.NewElbowFitting(list);
						list.Clear();
						list.Add(hycableTray2.CableTray);
						list.Add(newCableTray[1].CableTray);
						this.NewElbowFitting(list);
						break;
					}
					case 23:
					{
						List<HYCableTray<Conduit>> newCableTray = this.GetNewCableTray(list2, doc);
						if (hycableTray.IsPointOnCableTrayCurve(newCableTray[0].CustomConnectorStart.XYZ) || hycableTray.IsPointOnCableTrayCurve(newCableTray[0].CustomConnectorEnd.XYZ))
						{
							list.Add(hycableTray.CableTray);
							list.Add(hycableTray.CableTray);
							list.Add(newCableTray[0].CableTray);
							this.NewTeeFitting(list);
							list.Clear();
							list.Add(hycableTray2.CableTray);
							list.Add(newCableTray[1].CableTray);
							this.NewElbowFitting(list);
						}
						else
						{
							list.Add(hycableTray.CableTray);
							list.Add(newCableTray[0].CableTray);
							this.NewElbowFitting(list);
							list.Clear();
							list.Add(hycableTray2.CableTray);
							list.Add(hycableTray2.CableTray);
							list.Add(newCableTray[1].CableTray);
							this.NewTeeFitting(list);
						}
						break;
					}
					case 24:
					{
						List<HYCableTray<Conduit>> newCableTray = this.GetNewCableTray(list2, doc);
						list.Add(hycableTray.CableTray);
						list.Add(hycableTray.CableTray);
						list.Add(newCableTray[0].CableTray);
						this.NewTeeFitting(list);
						list.Clear();
						list.Add(hycableTray2.CableTray);
						list.Add(hycableTray2.CableTray);
						list.Add(newCableTray[1].CableTray);
						this.NewTeeFitting(list);
						break;
					}
					}
					result = true;
				}
				return result;
			}
			throw new Exception("没有找到适合的线管匹配类型!");
		}

		private bool IsCreateElbow(HYCableTray<Conduit> ct, XYZ pt)
		{
			bool result = false;
			if (ct.EndXYZ.DistanceTo(pt) < 0.1 || ct.StartXYZ.DistanceTo(pt) < 0.1)
			{
				result = true;
			}
			return result;
		}

		private IList<HYCableTray<Conduit>> SomeConduitOrderBy(IList<Conduit> conduits)
		{
			IList<HYCableTray<Conduit>> list = new List<HYCableTray<Conduit>>();
			switch (conduits.Count)
			{
			case 1:
				throw new Exception("请选择多根线管");
			case 2:
			{
				HYCableTray<Conduit> hycableTray = new HYCableTray<Conduit>(conduits[0]);
				HYCableTray<Conduit> hycableTray2 = new HYCableTray<Conduit>(conduits[1]);
				if (hycableTray.IsPipingOnLine(hycableTray2.CableTray))
				{
					IList<Conduit> list2 = new List<Conduit>();
					if (this.IsSameShape(hycableTray, hycableTray2))
					{
						hycableTray.Combine(hycableTray2.CableTray);
					}
					else
					{
						list2.Add(hycableTray.CableTray);
						list2.Add(hycableTray2.CableTray);
						this.NewTransitionFitting(list2);
					}
				}
				else
				{
					list.Add(hycableTray);
					list.Add(hycableTray2);
				}
				break;
			}
			case 3:
			{
				HYCableTray<Conduit> hyConduit = new HYCableTray<Conduit>(conduits[0]);
				HYCableTray<Conduit> hyConduit2 = new HYCableTray<Conduit>(conduits[1]);
				HYCableTray<Conduit> hyConduit3 = new HYCableTray<Conduit>(conduits[2]);
				list = this.OrderThreeConduit(hyConduit, hyConduit2, hyConduit3);
				break;
			}
			case 4:
			{
				IList<Conduit> list3 = new List<Conduit>();
				int num = 0;
				HYCableTray<Conduit> hycableTray3 = new HYCableTray<Conduit>(conduits[0]);
				HYCableTray<Conduit> hycableTray4 = new HYCableTray<Conduit>(conduits[1]);
				HYCableTray<Conduit> hycableTray5 = new HYCableTray<Conduit>(conduits[2]);
				HYCableTray<Conduit> hycableTray6 = new HYCableTray<Conduit>(conduits[3]);
				if (hycableTray3.IsPipingOnLine(hycableTray4.CableTray))
				{
					if (this.IsSameShape(hycableTray3, hycableTray4))
					{
						hycableTray3.Combine(hycableTray4.CableTray);
						num++;
					}
					if (this.IsSameShape(hycableTray5, hycableTray6))
					{
						hycableTray5.Combine(hycableTray6.CableTray);
						num += 2;
					}
					if (num == 0)
					{
						try
						{
							this.NewCrossFitting(conduits);
							break;
						}
						catch (Exception)
						{
							break;
						}
					}
					if (1 == num)
					{
						if (!hycableTray3.IsIntersection(hycableTray5.CableTray))
						{
							throw new Exception("请检查线管是否具有不同的尺寸，并且在不同的平面上");
						}
						list3.Add(hycableTray3.CableTray);
						list3.Add(hycableTray3.CableTray);
						list3.Add(hycableTray5.CableTray);
						list3.Add(hycableTray6.CableTray);
						this.NewCrossFitting(list3);
					}
					else if (2 == num)
					{
						if (!hycableTray3.IsIntersection(hycableTray5.CableTray))
						{
							throw new Exception("请检查线管是否具有不同的尺寸，并且在不同的平面上");
						}
						list3.Add(hycableTray3.CableTray);
						list3.Add(hycableTray4.CableTray);
						list3.Add(hycableTray5.CableTray);
						list3.Add(hycableTray5.CableTray);
						this.NewCrossFitting(list3);
					}
					else if (3 == num)
					{
						list.Add(hycableTray3);
						list.Add(hycableTray5);
					}
				}
				else if (hycableTray3.IsPipingOnLine(hycableTray5.CableTray))
				{
					if (this.IsSameShape(hycableTray3, hycableTray5))
					{
						hycableTray3.Combine(hycableTray5.CableTray);
						num++;
					}
					if (this.IsSameShape(hycableTray4, hycableTray6))
					{
						hycableTray4.Combine(hycableTray6.CableTray);
						num += 2;
					}
					if (num == 0)
					{
						this.NewCrossFitting(conduits);
					}
					else if (1 == num)
					{
						if (!hycableTray3.IsIntersection(hycableTray4.CableTray))
						{
							throw new Exception("请检查线管是否具有不同的尺寸，并且在不同的平面上");
						}
						list3.Add(hycableTray3.CableTray);
						list3.Add(hycableTray3.CableTray);
						list3.Add(hycableTray4.CableTray);
						list3.Add(hycableTray6.CableTray);
						this.NewCrossFitting(list3);
					}
					else if (2 == num)
					{
						if (!hycableTray3.IsIntersection(hycableTray4.CableTray))
						{
							throw new Exception("请检查线管是否具有不同的尺寸，并且在不同的平面上");
						}
						list3.Add(hycableTray3.CableTray);
						list3.Add(hycableTray5.CableTray);
						list3.Add(hycableTray4.CableTray);
						list3.Add(hycableTray4.CableTray);
						this.NewCrossFitting(list3);
					}
					else if (3 == num)
					{
						list.Add(hycableTray3);
						list.Add(hycableTray4);
					}
				}
				else if (hycableTray3.IsPipingOnLine(hycableTray6.CableTray))
				{
					if (this.IsSameShape(hycableTray3, hycableTray6))
					{
						hycableTray3.Combine(hycableTray6.CableTray);
						num++;
					}
					if (this.IsSameShape(hycableTray4, hycableTray5))
					{
						hycableTray4.Combine(hycableTray5.CableTray);
						num += 2;
					}
					if (num == 0)
					{
						this.NewCrossFitting(conduits);
					}
					else if (1 == num)
					{
						if (!hycableTray3.IsIntersection(hycableTray4.CableTray))
						{
							throw new Exception("请检查线管是否具有不同的尺寸，并且在不同的平面上");
						}
						list3.Add(hycableTray3.CableTray);
						list3.Add(hycableTray3.CableTray);
						list3.Add(hycableTray4.CableTray);
						list3.Add(hycableTray5.CableTray);
						this.NewCrossFitting(list3);
					}
					else if (2 == num)
					{
						if (!hycableTray3.IsIntersection(hycableTray4.CableTray))
						{
							throw new Exception("请检查线管是否具有不同的尺寸，并且在不同的平面上");
						}
						list3.Add(hycableTray3.CableTray);
						list3.Add(hycableTray5.CableTray);
						list3.Add(hycableTray4.CableTray);
						list3.Add(hycableTray4.CableTray);
						this.NewCrossFitting(list3);
					}
					else if (3 == num)
					{
						list.Add(hycableTray3);
						list.Add(hycableTray4);
					}
				}
				else if (hycableTray4.IsPipingOnLine(hycableTray5.CableTray))
				{
					if (this.IsSameShape(hycableTray4, hycableTray5))
					{
						hycableTray4.Combine(hycableTray5.CableTray);
						num++;
					}
					if (this.IsSameShape(hycableTray3, hycableTray6))
					{
						hycableTray3.Combine(hycableTray6.CableTray);
						num += 2;
					}
					if (num == 0)
					{
						this.NewCrossFitting(conduits);
					}
					else if (1 == num)
					{
						if (!hycableTray3.IsIntersection(hycableTray4.CableTray))
						{
							throw new Exception("请检查线管是否具有不同的尺寸，并且在不同的平面上");
						}
						list3.Add(hycableTray4.CableTray);
						list3.Add(hycableTray4.CableTray);
						list3.Add(hycableTray3.CableTray);
						list3.Add(hycableTray6.CableTray);
						this.NewCrossFitting(list3);
					}
					else if (2 == num)
					{
						if (!hycableTray3.IsIntersection(hycableTray4.CableTray))
						{
							throw new Exception("请检查线管是否具有不同的尺寸，并且在不同的平面上");
						}
						list3.Add(hycableTray4.CableTray);
						list3.Add(hycableTray5.CableTray);
						list3.Add(hycableTray3.CableTray);
						list3.Add(hycableTray3.CableTray);
						this.NewCrossFitting(list3);
					}
					else if (3 == num)
					{
						list.Add(hycableTray3);
						list.Add(hycableTray4);
					}
				}
				else if (hycableTray4.IsPipingOnLine(hycableTray6.CableTray))
				{
					if (this.IsSameShape(hycableTray4, hycableTray6))
					{
						hycableTray4.Combine(hycableTray6.CableTray);
						num++;
					}
					if (this.IsSameShape(hycableTray3, hycableTray5))
					{
						hycableTray3.Combine(hycableTray5.CableTray);
						num += 2;
					}
					if (num == 0)
					{
						this.NewCrossFitting(conduits);
					}
					else if (1 == num)
					{
						if (!hycableTray3.IsIntersection(hycableTray4.CableTray))
						{
							throw new Exception("请检查线管是否具有不同的尺寸，并且在不同的平面上");
						}
						list3.Add(hycableTray4.CableTray);
						list3.Add(hycableTray4.CableTray);
						list3.Add(hycableTray3.CableTray);
						list3.Add(hycableTray5.CableTray);
						this.NewCrossFitting(list3);
					}
					else if (2 == num)
					{
						if (!hycableTray3.IsIntersection(hycableTray4.CableTray))
						{
							throw new Exception("请检查线管是否具有不同的尺寸，并且在不同的平面上");
						}
						list3.Add(hycableTray4.CableTray);
						list3.Add(hycableTray6.CableTray);
						list3.Add(hycableTray3.CableTray);
						list3.Add(hycableTray3.CableTray);
						this.NewCrossFitting(list3);
					}
					else if (3 == num)
					{
						list.Add(hycableTray3);
						list.Add(hycableTray4);
					}
				}
				else if (hycableTray5.IsPipingOnLine(hycableTray6.CableTray))
				{
					if (this.IsSameShape(hycableTray5, hycableTray6))
					{
						hycableTray5.Combine(hycableTray6.CableTray);
						num++;
					}
					if (this.IsSameShape(hycableTray3, hycableTray4))
					{
						hycableTray3.Combine(hycableTray4.CableTray);
						num += 2;
					}
					if (num == 0)
					{
						this.NewCrossFitting(conduits);
					}
					else if (1 == num)
					{
						if (!hycableTray3.IsIntersection(hycableTray5.CableTray))
						{
							throw new Exception("请检查线管是否具有不同的尺寸，并且在不同的平面上");
						}
						list3.Add(hycableTray5.CableTray);
						list3.Add(hycableTray5.CableTray);
						list3.Add(hycableTray3.CableTray);
						list3.Add(hycableTray4.CableTray);
						this.NewCrossFitting(list3);
					}
					else if (2 == num)
					{
						if (!hycableTray3.IsIntersection(hycableTray5.CableTray))
						{
							throw new Exception("请检查线管是否具有不同的尺寸，并且在不同的平面上");
						}
						list3.Add(hycableTray5.CableTray);
						list3.Add(hycableTray6.CableTray);
						list3.Add(hycableTray3.CableTray);
						list3.Add(hycableTray3.CableTray);
						this.NewCrossFitting(list3);
					}
					else if (3 == num)
					{
						list.Add(hycableTray3);
						list.Add(hycableTray5);
					}
				}
				else if (hycableTray3.IsIntersection(hycableTray4.CableTray))
				{
					list.Add(hycableTray3);
					list.Add(hycableTray4);
				}
				else if (hycableTray3.IsIntersection(hycableTray5.CableTray))
				{
					list.Add(hycableTray3);
					list.Add(hycableTray5);
				}
				else if (hycableTray3.IsIntersection(hycableTray6.CableTray))
				{
					list.Add(hycableTray3);
					list.Add(hycableTray6);
				}
				else if (hycableTray4.IsIntersection(hycableTray5.CableTray))
				{
					list.Add(hycableTray4);
					list.Add(hycableTray5);
				}
				else if (hycableTray4.IsIntersection(hycableTray6.CableTray))
				{
					list.Add(hycableTray4);
					list.Add(hycableTray6);
				}
				else if (hycableTray5.IsIntersection(hycableTray6.CableTray))
				{
					list.Add(hycableTray5);
					list.Add(hycableTray6);
				}
				else
				{
					list.Add(hycableTray3);
					list.Add(hycableTray4);
				}
				break;
			}
			default:
				throw new Exception("线管不能超过四根");
			}
			return list;
		}

		private IList<HYCableTray<Conduit>> OrderThreeConduit(HYCableTray<Conduit> hyConduit, HYCableTray<Conduit> hyConduit1, HYCableTray<Conduit> hyConduit2)
		{
			IList<HYCableTray<Conduit>> list = new List<HYCableTray<Conduit>>();
			IList<Conduit> list2 = new List<Conduit>();
			list2.Add(hyConduit.CableTray);
			list2.Add(hyConduit1.CableTray);
			list2.Add(hyConduit2.CableTray);
			if (hyConduit.IsPipingOnLine(hyConduit1.CableTray))
			{
				if (this.IsSameShape(hyConduit, hyConduit1))
				{
					hyConduit.Combine(hyConduit1.CableTray);
					list.Add(hyConduit);
					list.Add(hyConduit2);
				}
				else
				{
					try
					{
						this.NewTeeFitting(list2);
					}
					catch (Exception)
					{
					}
					list = null;
				}
			}
			else if (hyConduit.IsPipingOnLine(hyConduit2.CableTray))
			{
				if (this.IsSameShape(hyConduit, hyConduit2))
				{
					hyConduit.Combine(hyConduit2.CableTray);
					list.Add(hyConduit);
					list.Add(hyConduit1);
				}
				else
				{
					try
					{
						this.NewTeeFitting(list2);
					}
					catch (Exception)
					{
					}
					list = null;
				}
			}
			else if (hyConduit1.IsPipingOnLine(hyConduit2.CableTray))
			{
				if (this.IsSameShape(hyConduit1, hyConduit2))
				{
					hyConduit1.Combine(hyConduit2.CableTray);
					list.Add(hyConduit);
					list.Add(hyConduit1);
				}
				else
				{
					try
					{
						this.NewTeeFitting(list2);
					}
					catch (Exception)
					{
					}
					list = null;
				}
			}
			else if (hyConduit.IsIntersection(hyConduit1.CableTray))
			{
				list.Add(hyConduit);
				list.Add(hyConduit1);
			}
			else if (hyConduit.IsIntersection(hyConduit2.CableTray))
			{
				list.Add(hyConduit);
				list.Add(hyConduit2);
			}
			else if (hyConduit1.IsIntersection(hyConduit2.CableTray))
			{
				list.Add(hyConduit2);
				list.Add(hyConduit1);
			}
			else
			{
				list.Add(hyConduit);
				list.Add(hyConduit1);
			}
			return list;
		}

		private bool IsSameShape(HYCableTray<Conduit> hyConduit1, HYCableTray<Conduit> hyConduit2)
		{
			bool result = false;
			if (hyConduit2.CableTray.get_Parameter(BuiltInParameter.RBS_CONDUIT_DIAMETER_PARAM) != null)
			{
				result = (hyConduit2.CustomConnectorStart.Connector.Radius == hyConduit1.CustomConnectorStart.Connector.Radius);
			}
			return result;
		}

		private List<HYCableTray<Conduit>> GetNewCableTray(IList<HYCableTray<Conduit>> hyCableTrays, Document doc)
		{
			List<HYCableTray<Conduit>> list = new List<HYCableTray<Conduit>>();
			XYZ defferentPlaneInstersection = hyCableTrays[0].GetDefferentPlaneInstersection(hyCableTrays[1].CableTray);
			XYZ defferentPlaneInstersection2 = hyCableTrays[1].GetDefferentPlaneInstersection(hyCableTrays[0].CableTray);
			Conduit sourceConduit = (hyCableTrays[0].GetDiameter() < hyCableTrays[1].GetDiameter()) ? hyCableTrays[0].CableTray : hyCableTrays[1].CableTray;
			Connector appropriateConnector = hyCableTrays[0].GetAppropriateConnector(defferentPlaneInstersection);
			Connector appropriateConnector2 = hyCableTrays[1].GetAppropriateConnector(defferentPlaneInstersection2);
			if (appropriateConnector.IsConnected && !hyCableTrays[0].IsPointOnCableTrayCurve(defferentPlaneInstersection))
			{
				Connector distanceConnector = hyCableTrays[0].GetDistanceConnector(defferentPlaneInstersection);
				XYZ xyz = (distanceConnector.Origin - appropriateConnector.Origin).Normalize();
				double num = distanceConnector.Origin.DistanceTo(defferentPlaneInstersection) + 0.3;
				XYZ startXYZ = defferentPlaneInstersection + xyz * num;
				XYZ endXYZ = defferentPlaneInstersection2 + xyz * num;
				Conduit conduit = HYConduitConnect.NewConduit(sourceConduit, startXYZ, endXYZ);
				Conduit conduit2 = HYConduitConnect.NewConduit(sourceConduit, defferentPlaneInstersection2, endXYZ);
				this.NewElbowFitting(new List<Conduit>
				{
					conduit,
					conduit2
				});
				list.Add(new HYCableTray<Conduit>(conduit));
				list.Add(new HYCableTray<Conduit>(conduit2));
			}
			else if (appropriateConnector2.IsConnected && !hyCableTrays[1].IsPointOnCableTrayCurve(defferentPlaneInstersection2))
			{
				Connector distanceConnector2 = hyCableTrays[1].GetDistanceConnector(defferentPlaneInstersection2);
				XYZ xyz2 = (distanceConnector2.Origin - appropriateConnector2.Origin).Normalize();
				double num2 = distanceConnector2.Origin.DistanceTo(defferentPlaneInstersection2) + 0.3;
				XYZ xyz3 = defferentPlaneInstersection + xyz2 * num2;
				XYZ endXYZ2 = defferentPlaneInstersection2 + xyz2 * num2;
				Conduit conduit3 = HYConduitConnect.NewConduit(sourceConduit, xyz3, endXYZ2);
				Conduit conduit4 = HYConduitConnect.NewConduit(sourceConduit, defferentPlaneInstersection, xyz3);
				this.NewElbowFitting(new List<Conduit>
				{
					conduit3,
					conduit4
				});
				list.Add(new HYCableTray<Conduit>(conduit4));
				list.Add(new HYCableTray<Conduit>(conduit3));
			}
			else
			{
				HYCableTray<Conduit> item = new HYCableTray<Conduit>(HYConduitConnect.NewConduit(sourceConduit, defferentPlaneInstersection, defferentPlaneInstersection2));
				list.Add(item);
				list.Add(item);
			}
			return list;
		}

		public static Conduit NewConduit(Conduit sourceConduit, XYZ startXYZ, XYZ endXYZ)
		{
			Conduit conduit = null;
			if (sourceConduit == null || startXYZ == null || endXYZ == null)
			{
				return null;
			}
			Document document = sourceConduit.Document;
			foreach (ElementId elementId in ElementTransformUtils.CopyElement(document, sourceConduit.Id, new XYZ(0.0, 0.0, 0.0)))
			{
				conduit = (document.GetElement(elementId) as Conduit);
			}
			document.Regenerate();
			if (conduit != null)
			{
				Line curve = Line.CreateBound(startXYZ, endXYZ);
				(conduit.Location as LocationCurve).Curve = curve;
			}
			document.Regenerate();
			return conduit;
		}

		private IList<XYZ> GetPoint21(IList<HYCableTray<Conduit>> hyCableTrays)
		{
			IList<XYZ> list = new List<XYZ>();
			IList<Connector> appropriateConnector = hyCableTrays[0].GetAppropriateConnector(hyCableTrays[1].CableTray);
			XYZ xyz = null;
			if (appropriateConnector != null && appropriateConnector.Count > 1)
			{
				if (!appropriateConnector[0].IsConnected && !appropriateConnector[1].IsConnected)
				{
					xyz = (appropriateConnector[0].Origin + appropriateConnector[1].Origin) / 2.0;
				}
				else if (appropriateConnector[0].IsConnected)
				{
					xyz = hyCableTrays[0].GetDistanceConnector(appropriateConnector[0].Origin).Origin;
				}
				else
				{
					xyz = hyCableTrays[1].GetDistanceConnector(appropriateConnector[1].Origin).Origin;
				}
			}
			if (xyz != null)
			{
				XYZ project = hyCableTrays[0].GetProject(xyz, true);
				XYZ project2 = hyCableTrays[1].GetProject(xyz, true);
				XYZ item = new XYZ(project2.X, project2.Y, project.Z);
				list.Add(project);
				list.Add(project2);
				list.Add(item);
			}
			return list;
		}

		private Conduit GetPointOnConduit(IList<Conduit> hyConduits)
		{
			Conduit result = null;
			HYCableTray<Conduit> hycableTray = new HYCableTray<Conduit>(hyConduits[0]);
			HYCableTray<Conduit> hycableTray2 = new HYCableTray<Conduit>(hyConduits[1]);
			if (hyConduits.Count == 2)
			{
				XYZ xyz = hycableTray.IntersectionPoint(hycableTray2.CableTray);
				if (xyz != null)
				{
					if (hycableTray.IsPointOnCableTrayCurve(xyz))
					{
						result = hycableTray.CableTray;
					}
					else if (hycableTray2.IsPointOnCableTrayCurve(xyz))
					{
						result = hycableTray2.CableTray;
					}
				}
			}
			return result;
		}

		private IList<Conduit> OrderConduitPosition(IList<Conduit> conduits)
		{
			IList<Conduit> list = new List<Conduit>();
			new List<XYZ>();
			HYCableTray<Conduit> hycableTray = new HYCableTray<Conduit>(conduits[0]);
			HYCableTray<Conduit> hycableTray2 = new HYCableTray<Conduit>(conduits[1]);
			HYCableTray<Conduit> hycableTray3 = new HYCableTray<Conduit>(conduits[2]);
			if (3 == conduits.Count)
			{
				if (hycableTray.IsCableTrayOnLine(hycableTray2.CableTray) || hycableTray.IsParallel(hycableTray2.CableTray))
				{
					list.Add(hycableTray3.CableTray);
					list.Add(hycableTray.CableTray);
					list.Add(hycableTray2.CableTray);
				}
				else if (hycableTray.IsCableTrayOnLine(hycableTray3.CableTray) || hycableTray.IsCableTrayOnLine(hycableTray3.CableTray))
				{
					list.Add(hycableTray2.CableTray);
					list.Add(hycableTray.CableTray);
					list.Add(hycableTray3.CableTray);
				}
				else
				{
					list = conduits;
				}
			}
			else if (4 == conduits.Count)
			{
				HYCableTray<Conduit> hycableTray4 = new HYCableTray<Conduit>(conduits[3]);
				if (hycableTray.IsCableTrayOnLine(hycableTray2.CableTray))
				{
					list = conduits;
				}
				else if (hycableTray.IsCableTrayOnLine(hycableTray3.CableTray))
				{
					list.Add(hycableTray.CableTray);
					list.Add(hycableTray3.CableTray);
					list.Add(hycableTray2.CableTray);
					list.Add(hycableTray4.CableTray);
				}
				else if (hycableTray.IsCableTrayOnLine(hycableTray4.CableTray))
				{
					list.Add(hycableTray.CableTray);
					list.Add(hycableTray4.CableTray);
					list.Add(hycableTray2.CableTray);
					list.Add(hycableTray3.CableTray);
				}
				else
				{
					list = conduits;
				}
			}
			return list;
		}
	}
}
